#include "resourcemngr.h"
#include "core.h"

resourcemngr::resourcemngr()
{
	m_textures.clear();
	m_models.clear();
	m_fonts.clear();
}

resourcemngr::~resourcemngr()
{
	unload();
}

void resourcemngr::load(xml_node& _node)
{

}

void resourcemngr::unload()
{
	map<string,texture*>::iterator i;
	for(i=m_textures.begin();i!=m_textures.end();i++)
	{
		delete (*i).second;
	}
	m_textures.clear();

	map<string,model*>::iterator j;
	for(j=m_models.begin();j!=m_models.end();j++)
	{
		delete (*j).second;
	}
	m_models.clear();

	map<string,font*>::iterator k;
	for(k=m_fonts.begin();k!=m_fonts.end();k++)
	{
		delete (*k).second;
	}
	m_fonts.clear();
}

texture* resourcemngr::get_texture(const char* _fname)
{
	map<string,texture*>::iterator i;
	i = m_textures.find(_fname); 
	if(i!=m_textures.end()) 
		return ((*i).second); 
	string path = pathmngr::get_inst().get_texture(_fname);
	//#TODO
	texture* tex = core::get_inst().get_render()->create_texture(8,8,dxt1);
	m_textures[_fname] = tex;
	return tex;
}

model* resourcemngr::get_model(const char* _fname)
{
	map<string,model*>::iterator j;
	j = m_models.find(_fname); 
	if(j!=m_models.end()) 
		return ((*j).second); 
	string path = pathmngr::get_inst().get_model(_fname);
	model* mdl  = new model();
	mdl->load_ms3d(path.c_str());
	m_models[_fname] = mdl;
	return mdl;
}

font* resourcemngr::get_font(const char* _fname)
{
	map<string,font*>::iterator k;
	k = m_fonts.find(_fname); 
	if(k!=m_fonts.end()) 
		return ((*k).second); 
	string path = pathmngr::get_inst().get_font(_fname);
	font* fnt   = new font(path.c_str());
	m_fonts[_fname] = fnt;
	return fnt;
}